package com.sui.bigdata.sml.web.controller;

import com.sui.bigdata.sml.web.dto.ExperimentDTO;
import com.sui.bigdata.sml.web.dto.ProjectDTO;
import com.sui.bigdata.sml.web.service.ExperimentService;
import com.sui.bigdata.sml.web.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author songhaicheng
 * @date 2019/11/6 10:50
 * @description
 * @reviewer
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/web/ui/experiments")
@Slf4j
public class ExperimentController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExperimentController.class);
    @Autowired
    private ExperimentService service;
    @Autowired
    private ProjectService projectService;

    @PostMapping
    ResponseEntity add(@RequestBody ExperimentDTO dto) {
        return new ResponseEntity<>(dto.setId(service.add(dto)), HttpStatus.CREATED);
    }

    @GetMapping
    ResponseEntity listByProjectId(@RequestParam("project_id") Long projectId) {
        LOGGER.info("请求列出项目列表参数project_id值为：{}",projectId);
        ProjectDTO project = projectService.get(projectId);
        return new ResponseEntity<>(service.listByProject(project, false), HttpStatus.OK);
    }

    @PutMapping("/{id}")
    ResponseEntity update(@PathVariable("id") Long id, @RequestBody ExperimentDTO dto) {
        dto.setId(id);
        service.update(dto);
        return new ResponseEntity<>(dto, HttpStatus.OK);
    }

    @DeleteMapping("/{id}")
    ResponseEntity delete(@PathVariable("id") Long id) {
        service.delete(id);
        log.info("实验删除成功（不可见），id：{}", id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
